from PySide6.QtCore import QDateTime
from Devices.Chiot.BsDevs.ComDev.ComDevParser import ComDevParser
import json

class SensorParser(ComDevParser):
    def ParseHeartbeat(msgValue, fullStatus):            
        ret, value = SensorParser.GetIntProperty(msgValue, "adcConvert")
        if ret:
            fullStatus.adcConvert = value

        ret, value = SensorParser.GetIntProperty(msgValue, "adcOrigin")
        if ret:
            fullStatus.adcOrigin = value

        ret, value = SensorParser.GetIntProperty(msgValue, "adcRefer")
        if ret:
            fullStatus.adcRefer = value

        ret, value = SensorParser.GetIntProperty(msgValue, "adcCali")
        if ret:
            fullStatus.adcCali = value

        ret, value = SensorParser.GetIntProperty(msgValue, "adcFactor")
        if ret:
            fullStatus.adcFactor = value

        ret, value = SensorParser.GetIntProperty(msgValue, "adcIntercept")
        if ret:
            fullStatus.adcIntercept = value


        if not "stateValue" in msgValue:
            return True
        stateValue = msgValue["stateValue"]

        # TODO 
        ret, value = SensorParser.GetIntProperty(stateValue, "pir", "status")
        if ret:
            fullStatus.sensorStatus = value
            fullStatus.sensorStatusMsg = "有人移动" if value else "无人移动"
            fullStatus.changeTime = QDateTime.currentSecsSinceEpoch()

        ret, value = SensorParser.GetIntProperty(stateValue, "door", "status")
        if ret:
            fullStatus.sensorStatus = value
            fullStatus.sensorStatusMsg = "打开" if value else "关闭"
            fullStatus.changeTime = QDateTime.currentSecsSinceEpoch()
        
        ret, value = SensorParser.GetIntProperty(stateValue, "sos", "status")
        if ret:
            fullStatus.sensorStatus = value
            fullStatus.sensorStatusMsg = "sos告警" if value else "未告警"
            fullStatus.changeTime = QDateTime.currentSecsSinceEpoch()
        
        ret, value = SensorParser.GetIntProperty(stateValue, "waterLeak", "status")
        if ret:
            fullStatus.sensorStatus = value
            fullStatus.sensorStatusMsg = "浸水" if value else "未浸水"
            fullStatus.changeTime = QDateTime.currentSecsSinceEpoch()
        

        ret, value = SensorParser.GetIntProperty(stateValue, "electric", "powerWarn")
        if ret:
            fullStatus.powerWarn = value

        ret, value = SensorParser.GetIntProperty(stateValue, "electric", "batteryPercentage")
        if ret:
            fullStatus.batteryPercentage = value

        ret, value = SensorParser.GetIntProperty(stateValue, "checkStatus", "status")
        if ret:
            fullStatus.checkStatus = value

        ret, value = SensorParser.GetIntProperty(stateValue, "checkStatusTime", "timestamp")
        if ret:
            fullStatus.checkStatusTime = value

        return True

    # data = {"client":"NetworkTest", "time":int(round(time.time() * 1000)),"setAdcParam": {}}
    def PackSetAdcParam(paramName, paramValue):
        data = {}
        data["client"] = "NetworkTest"
        data["time"] = QDateTime.currentSecsSinceEpoch()
        data["cmd"] = "setAdcParam"
        data[paramName] = paramValue

        
        return json.dumps(data, separators=(',', ':'))